package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.constant.InstructionService;
import com.ruoyi.system.domain.LogEntry;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/sys/etcAuto")
public class EtcAutoController extends BaseController {

    @Autowired
    private InstructionService instructionService;

    @GetMapping("/autoPull")
    public AjaxResult autoPull(@RequestBody List<LogEntry> lists) throws MqttException, InterruptedException {
        // 调用异步方法并立即返回响应给客户端
        instructionService.executeInstructionsSequence(lists)
                .thenRun(() -> System.out.println("All instructions have been executed."))
                .exceptionally(ex -> {
                    ex.printStackTrace();
                    return null;
                });

        return success(true);
    }
}